package cn.shutdown.compress.controller;

import cn.shutdown.compress.config.ImgZipConfig;
import cn.shutdown.compress.utils.ThumbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
public class CompressController {

    private static final Logger logger = LoggerFactory.getLogger(CompressController.class);

    @RequestMapping("/")
    public String hello() throws Exception {
        long start = System.currentTimeMillis();
        submit(ImgZipConfig.imagePath);
        return (System.currentTimeMillis() - start) / 1000 / 60 + ":分";
    }

    public void submit(String path) throws Exception {
        logger.info("start submit");
        File file = new File(path);
        if (file.exists() && file.isDirectory()) {
            File[] uploads = file.listFiles();
            if (uploads == null) {
                return;
            }
            for (File upload : uploads) {
                if (upload.isFile()) {
                    ThumbUtils.imgUploadCompress(upload, null, null);
                }
            }
        }
        logger.info("end submit");
    }

}
